package com.fuja.fumo.ui;
import org.apache.log4j.Logger;

import com.fuja.swf.ui.UIUtil;

import waba.fx.Color;
import waba.io.File;
import waba.sys.Convert;
import waba.sys.Settings;
import waba.ui.*;
public class UISelect extends Window{
	private static Logger log = Logger.getLogger(UISelect.class);
	private Label label = new Label("Select one test: ");
	private ListBox lbTests;
	private Button btnLoad = new Button("Load");
	private Button btnLoadExternal = new Button("Extenal");
	public void onStart(){
		label.setBackColor(Color.BLACK);
		label.setForeColor(Color.WHITE);
		add(label);
		label.setRect(LEFT,TOP, Settings.screenWidth, PREFERRED);		
		
		lbTests = new ListBox(
			new String[]{
				"ENGLISH 1",
				"SCEA 1",
				"SCEA 2",
				"SCJP 1",
				"SCJP 2",
				"SCJP 3",
				"SCJP 4",
				"SCBCD KJE 1",
				"SCBCD KJE 2",
				"SCBCD KJE 3",
				"SCBCD KJE 4",
				"SCBCD KJE 5",
				"SCBCD KJE 6",
				"SCBCD NICKY 1",
				"SCBCD NICKY 2",
				"SCBCD NICKY 3",
				"SCBCD NICKY 4",
				"SCBCD NICKY 5",
				"SCBCD NICKY 6",
				"SCBCD NICKY 7"
			}
		);
		add(lbTests);		
		lbTests.setRect(LEFT,AFTER+2,FILL,FILL-20);
		add(btnLoad,RIGHT - 1,BOTTOM -1);	
		add(btnLoadExternal,LEFT +1 ,SAME);
		
		addToolTips();
		
	}
	private void addToolTips(){
		//Tips
		new ToolTip(lbTests,"Avaible Tests");
		new ToolTip(btnLoadExternal,"Load External File");
		new ToolTip(btnLoad,"Load Selected Test");
	}
	public void onEvent(Event e) {
		if ((e.target==btnLoad)&& (e.type==ControlEvent.PRESSED)){
			String value = lbTests.getSelectedItem().toString();
			if (value.equals(""))
				return;
			value = "xml/" 
				+ value.replace(' ','_').toLowerCase() 
				+ ".xml";
			
			try {				
				Window window = new UIQuestion(value);
				UIUtil.swap(window);
			} catch (Exception ex) {
				UIUtil.showMsg("Error", ex.getMessage());	
			}			
		}else if ((e.target==btnLoadExternal) && (e.type==ControlEvent.PRESSED)){
			InputDialog id = new InputDialog("External File","Enter external file path:","");
			id.popupBlockingModal();
			if ((id.getPressedButtonIndex()==0) && !id.getValue().equals("")){
				try {				
					File file = new File(id.getValue(),File.READ_ONLY);
					Window window = new UIQuestion(file);
					UIUtil.swap(window);
				} catch (Exception ex) {
					String aux = Convert.insertLineBreak(Settings.screenWidth-10, '|', fm,"Result: " +  ex.getMessage());
					UIUtil.showMsg("Error", aux);	
					log.error(aux);
				}	
			}
		}
	}
}
